home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / dviware / xdvi-dos / src / util.c < prev    next >
C/C++ Source or Header  |  1992-10-21  |  5KB  |  217 lines

  1. /*
  2.  * DVI previewer for X.
  3.  *
  4.  * Eric Cooper, CMU, September 1985.
  5.  *
  6.  * Code derived from dvi-imagen.c.
  7.  *
  8.  * Modification history:
  9.  * 1/1986    Modified for X.10    --Bob Scheifler, MIT LCS.
  10.  * 7/1988    Modified for X.11    --Mark Eichin, MIT
  11.  * 12/1988    Added 'R' option, toolkit, magnifying glass
  12.  *                    --Paul Vojta, UC Berkeley.
  13.  * 2/1989    Added tpic support    --Jeffrey Lee, U of Toronto
  14.  * 4/1989    Modified for System V    --Donald Richardson, Clarkson Univ.
  15.  * 3/1990    Added VMS support    --Scott Allendorf, U of Iowa
  16.  * 7/1990    Added reflection mode    --Michael Pak, Hebrew U of Jerusalem
  17.  * 1/1992    Added greyscale code    --Till Brychcy, Techn. Univ. Muenchen
  18.  *                      and Lee Hetherington, MIT
  19.  *
  20.  *    Compilation options:
  21.  *    SYSV    compile for System V
  22.  *    VMS    compile for VMS
  23.  *    X10    compile for X10
  24.  *    NOTOOL    compile without toolkit (X11 only)
  25.  *    BUTTONS    compile with buttons on the side of the window (needs toolkit)
  26.  *    MSBITFIRST    store bitmaps internally with most significant bit first
  27.  *    BMSHORT    store bitmaps in shorts instead of bytes
  28.  *    BMLONG    store bitmaps in longs instead of bytes
  29.  *    ALTFONT    default for -altfont option
  30.  *    A4    use European size paper
  31.  *    TEXXET    support reflection dvi codes (right-to-left typesetting)
  32.  */
  33.  
  34. #include "xdvi.h"
  35. #include <errno.h>
  36. int    errno;
  37.  
  38. #ifdef VMS
  39. #include <rmsdef.h>
  40. #endif /* VMS */
  41.  
  42. #ifndef    X_NOT_STDC_ENV
  43. #include <stdlib.h>
  44. #else
  45. char    *malloc();
  46. #endif
  47. #if    defined(macII) && !defined(__STDC__) /* stdlib.h doesn't define these */
  48. char    *malloc();
  49. #endif /* macII */
  50.  
  51. #if    NeedVarargsPrototypes        /* this is for oops */
  52. #include <stdarg.h>
  53. #else
  54. #include <varargs.h>
  55. #endif
  56.  
  57. #ifdef    DOPRNT    /* define this if vfprintf gives you trouble */
  58. #define    vfprintf(stream, message, args)    _doprnt(message, args, stream)
  59. #endif
  60.  
  61. /*
  62.  *    General utility routines.
  63.  */
  64.  
  65. /*
  66.  *    Print error message and quit.
  67.  */
  68.  
  69. #if    NeedVarargsPrototypes
  70. NORETURN void
  71. oops(_Xconst char *message, ...)
  72. #else
  73. /* VARARGS */
  74. NORETURN void
  75. oops(va_alist)
  76.     va_dcl
  77. #endif
  78. {
  79. #if    !NeedVarargsPrototypes
  80.     _Xconst char *message;
  81. #endif
  82.     va_list    args;
  83.  
  84.     Fprintf(stderr, "%s: ", prog);
  85. #if    NeedVarargsPrototypes
  86.     va_start(args, message);
  87. #else
  88.     va_start(args);
  89.     message = va_arg(args, _Xconst char *);
  90. #endif
  91.     (void) vfprintf(stderr, message, args);
  92.     va_end(args);
  93.     Putc('\n', stderr);
  94.     exit(1);
  95. }
  96.  
  97. /*
  98.  *    Either allocate storage or fail with explanation.
  99.  */
  100.  
  101. char *
  102. xmalloc(size, why)
  103.     unsigned    size;
  104.     _Xconst char    *why;
  105. {
  106.     char *mem = malloc(size);
  107.  
  108.     if (mem == NULL)
  109.         oops("! Cannot allocate %u bytes for %s.\n", size, why);
  110.     return mem;
  111. }
  112.  
  113. /*
  114.  *    Allocate bitmap for given font and character
  115.  */
  116.  
  117. void
  118. alloc_bitmap(bitmap)
  119.     register struct bitmap *bitmap;
  120. {
  121.     register unsigned int    size;
  122.  
  123.     /* width must be multiple of 16 bits for raster_op */
  124.     bitmap->bytes_wide = ROUNDUP(bitmap->w, BITS_PER_BMUNIT) *
  125.         BYTES_PER_BMUNIT;
  126.     size = bitmap->bytes_wide * bitmap->h;
  127.     bitmap->bits = xmalloc(size != 0 ? size : 1, "character bitmap");
  128. }
  129.  
  130.  
  131. /*
  132.  *    Close the pixel file for the least recently used font.
  133.  */
  134.  
  135. static    void
  136. close_a_file()
  137. {
  138.     register struct font *fontp;
  139.     unsigned short oldest = ~0;
  140.     struct font *f = NULL;
  141.  
  142.     for (fontp = font_head; fontp != NULL; fontp = fontp->next)
  143.         if (fontp->file != NULL && fontp->timestamp <= oldest) {
  144.         f = fontp;
  145.         oldest = fontp->timestamp;
  146.         }
  147.     if (f == NULL)
  148.         oops("Can't find an open pixel file to close");
  149.     Fclose(f->file);
  150.     f->file = NULL;
  151.     ++n_files_left;
  152. }
  153.  
  154. /*
  155.  *    Open a file in the given mode.
  156.  */
  157.  
  158. FILE *
  159. xfopen(filename)
  160.     _Xconst char    *filename;
  161. {
  162.     FILE    *f;
  163.  
  164.     if (n_files_left == 0) close_a_file();
  165.     f = fopen(filename, OPEN_MODE);
  166.  
  167. #ifndef VMS
  168. /* ericho  if (f == NULL && (errno == EMFILE || errno == ENFILE)) */
  169.   if (f == NULL && (errno == EMFILE))
  170. #else    /* VMS */
  171.     if (f == NULL && errno == EVMSERR && vaxc$errno == RMS$_ACC)
  172. #endif    /* VMS */
  173.  
  174.   {
  175.         n_files_left = 0;
  176.         close_a_file();
  177.         f = fopen(filename, OPEN_MODE);
  178.     }
  179.     return f;
  180. }
  181.  
  182.  
  183. /*
  184.  *
  185.  *      Read size bytes from the FILE fp, constructing them into a
  186.  *      signed/unsigned integer.
  187.  *
  188.  */
  189.  
  190. unsigned long
  191. num(fp, size)
  192.     register FILE *fp;
  193.     register int size;
  194. {
  195.     register long x = 0;
  196.  
  197.     while (size--) x = (x << 8) | one(fp);
  198.     return x;
  199. }
  200.  
  201. long
  202. snum(fp, size)
  203.     register FILE *fp;
  204.     register int size;
  205. {
  206.     register long x;
  207.  
  208. #ifdef    __STDC__
  209.     x = (signed char) getc(fp);
  210. #else
  211.     x = (unsigned char) getc(fp);
  212.     if (x & 0x80) x -= 0x100;
  213. #endif
  214.     while (--size) x = (x << 8) | one(fp);
  215.     return x;
  216. }
  217.